/**
 *
 */
package jp.takapy.baycity.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXSource;

import org.apache.http.protocol.HTTP;
import org.ccil.cowan.tagsoup.Parser;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

/**
 * @author Takapy
 *
 */
public class HtmlParseTestMain {
/*
	public static void main(String args[]) throws IOException, SAXException {

		String urlStr = "http://www.baycity-bus.co.jp/cgi-local/rosen_index.cgi?cd=010-H-13&nb=1";
		String encoding = "UTF-8";

		try {
			Document document = parse(urlStr, encoding);
			System.out.println("Operation Success !!");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}


	public static Document parse(String url, String encoding)
			throws IOException {

		if (url == null || url.length() == 0) {
			throw new IllegalArgumentException("urlがnullか空です");
		}

		if (encoding == null || encoding.length() == 0) {
			throw new IllegalArgumentException("encodingがnullか空です");
		}

		InputStream stream = null;
		try {
			stream = new URL(url).openStream();

			InputSource source = new InputSource(stream);
			source.setEncoding(encoding);

			try {
				XMLReader reader = new Parser();
				reader.setFeature(Parser.namespacesFeature, false);
				reader.setFeature(Parser.namespacePrefixesFeature, false);

				Transformer transformer = TransformerFactory.newInstance()
						.newTransformer();

				DOMResult result = new DOMResult();
				transformer.transform(new SAXSource(reader, source), result);

				return (Document) result.getNode();

			} catch (SAXException e) {

				// Parser.setFeatureは失敗しないはず。
				throw new RuntimeException(e);

			} catch (TransformerException e) {

				// これに失敗してるようではどうしようもない。
				throw new RuntimeException(e);
			}

		} finally {
			if (stream != null) {
				stream.close();
			}
		}
	}
*/
}
